package com.architecture.core.service.query;

import com.architecture.core.entity.PermissionRole;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.AbstractService;
import com.architecture.core.utils.ConfigUtil;
import com.architecture.core.utils.ErrCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午6:12
 * To change this template use File | Settings | File Templates.
 */
@Service("queryPermissionRoleService")
public class QueryPermissionRoleService extends AbstractService {

    public List<PermissionRole> findPermissionRolesByPermissionRoleId(String permissionRoleId) throws ErrException {

        PermissionRole role = (PermissionRole)super.baseDAO.findUnique(PermissionRole.class, permissionRoleId);

        try {
            return this.findAllRole(role);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_G);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId, String userId) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_G_U);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        paramMap.put("userId", userId);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId, int categoryType) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_G_C);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        paramMap.put("categoryType", categoryType);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId,String userId, int categoryType) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_G_U_C);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        paramMap.put("userId", userId);
        paramMap.put("categoryType", categoryType);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByUserId(String userId) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_U);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userId", userId);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByUserId(String userId, int categoryType) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_U_C);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userId", userId);
        paramMap.put("categoryType", categoryType);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByCategoryId(String categoryId) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_C);
        if (StringUtils.isBlank(ql)) {
            throw new ErrException(ErrCode.BLANK_STR);
        }
        ConfigUtil.getString(ConfigUtil.D_QUERY_ROLE_BY_C);
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("categoryId", categoryId);
        try {
            return this.findAllRole(super.baseDAO.find(ql, paramMap));
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByPermissionRoleId(String permissionRoleId,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionRolesByPermissionRoleId(permissionRoleId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionRolesByGroupId(groupId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId, String userId,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionRolesByGroupId(groupId,userId).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId, int categoryType,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionRolesByGroupId(groupId,categoryType).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId,String userId, int categoryType,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionRolesByGroupId(groupId,userId,categoryType).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByUserId(String userId,Integer firstIndex, Integer fetchSize) throws ErrException {

        try {
            return this.findPermissionRolesByUserId(userId).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByUserId(String userId, int categoryType,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionRolesByUserId(userId,categoryType).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByCategoryId(String categoryId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findPermissionRolesByCategoryId(categoryId).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByPermissionRoleId(String permissionRoleId,Integer firstIndex) throws ErrException {

        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByPermissionRoleId(permissionRoleId);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId,Integer firstIndex) throws ErrException {
        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByGroupId(groupId);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId, String userId,Integer firstIndex) throws ErrException {

        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByGroupId(groupId,userId);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId, int categoryType,Integer firstIndex) throws ErrException {
        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByGroupId(groupId,categoryType);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByGroupId(String groupId,String userId, int categoryType,Integer firstIndex) throws ErrException {
        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByGroupId(groupId,userId,categoryType);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByUserId(String userId,Integer firstIndex) throws ErrException {

        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByUserId(userId);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByUserId(String userId, int categoryType,Integer firstIndex) throws ErrException {
        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByUserId(userId,categoryType);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<PermissionRole> findPermissionRolesByCategoryId(String categoryId,Integer firstIndex) throws ErrException {
        try {
            List<PermissionRole> permissionRoleList = this.findPermissionRolesByCategoryId(categoryId);
            return permissionRoleList.subList(firstIndex, permissionRoleList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    /**
     * 递归查询所有角色
     *
     * @param roleList
     * @return
     */
    private List<PermissionRole> findAllRole(List<PermissionRole> roleList) {
        List<PermissionRole> newRoleList = new ArrayList<PermissionRole>();
        newRoleList.addAll(roleList);

        for (PermissionRole role : roleList) {
            this.addSubRole(role, newRoleList);
        }

        return newRoleList;
    }

    private List<PermissionRole> findAllRole(PermissionRole role) {
        List<PermissionRole> newRoleList = new ArrayList<PermissionRole>();
        newRoleList.add(role);

        this.addSubRole(role, newRoleList);

        return newRoleList;
    }

    /**
     * @param role
     * @param newRoleList 存储递归后所有对象
     */
    private void addSubRole(PermissionRole role, List<PermissionRole> newRoleList) {

        for (PermissionRole addRole : role.getPermissionRoleList()) {
            if (addRole.isDeleted()) {
                continue;
            } else {
                newRoleList.add(addRole);
                this.addSubRole(addRole, newRoleList);
            }
        }

    }
}